<?php
if (!isset($crud_table) || !isset($crud_key))
{
	echo 'Please declare the <strong>$crud_table</strong> variable containing the table name, '
		. 'and the <strong>$crud_key</strong> variable containing the primary key of that table.';
}
else
{
	// DCMS specific. We must stay on the same page.
	$persistent_query = '?';
	if (isset($_GET['p']))
		$persistent_query = '?p=' . urlencode($_GET['p']) . '&';
	
	$sensor = mysql_query("SELECT * FROM $crud_table;");
	
	$fields = array();
	$fieldnames = array();
	$fieldcount = mysql_num_fields($sensor);
	for ($i = 0; $i < $fieldcount; $i++)
	{
		$fields[mysql_field_name($sensor, $i)] = mysql_field_type($sensor, $i);
		$fieldnames[] = mysql_field_name($sensor, $i);
	}
	
	$fieldlist = '`' . implode('`, `', $fieldnames) . '`';
	$fieldheaders = '<tr><th>' . implode('</th><th>', $fieldnames) . '</th><th>&nbsp;</th></tr>';
	
	echo '<table class="thingreytable">';
	echo $fieldheaders;
	while ($row = mysql_fetch_array($sensor))
	{
		echo '<tr>';
		foreach ($fields as $name => $type)
		{
			$styleflags = '';
			if ($type == 'blob')
				$styleflags = ' style="width: 200px;" ';
			
			echo "<td $styleflags>";
			echo substr(htmlspecialchars($row[$name]), 0, 255);
			echo '</td>';
		}
		$editquery = $persistent_query . "act=edit&id=" . $row[$crud_key];
		$deletequery = $persistent_query . "act=delete&id=" . $row[$crud_key];
		echo "<td><a href=\"$editquery\">Edit</a> <a href=\"$deletequery\">Delete</a></td>";
		echo '</tr>';
	}
	
	echo '</table>';
	
	?>
	<a href="<?php echo $persistent_query; ?>act=new">New Record</a>
	<?php
}
?>
